package cn.tedu.management.portal.service.impl;

import cn.tedu.management.portal.dto.SalaryDTO;
import cn.tedu.management.portal.ex.MessageExist;
import cn.tedu.management.portal.ex.NotFoundException;
import cn.tedu.management.portal.mapper.AttendanceMapper;
import cn.tedu.management.portal.mapper.PerformanceMapper;
import cn.tedu.management.portal.model.Attendance;
import cn.tedu.management.portal.model.Performance;
import cn.tedu.management.portal.model.Salary;
import cn.tedu.management.portal.mapper.SalaryMapper;
import cn.tedu.management.portal.service.ISalaryService;
import cn.tedu.management.portal.vo.SalaryVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2020-12-08
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements ISalaryService {
    @Autowired
    SalaryMapper salaryMapper;

    @Autowired
    PerformanceMapper performanceMapper;
    @Autowired
    AttendanceMapper attendanceMapper;

    @Override
    public void addSalary(SalaryDTO salaryDTO) {

       Integer salaryId = salaryMapper.selectIdByStaffId(salaryDTO.getSalary().getStaffId());

        if(salaryId != null){
            throw new MessageExist("该用户工资信息已经存在！");
        }
        if(salaryDTO.getSalary()==null){
            throw new NotFoundException("工资信息没有插入");
        }

        LocalDateTime now = LocalDateTime.now();
        salaryDTO.getSalary().setGmtCreate(now);
        salaryDTO.getSalary().setGmtModified(now);
        salaryMapper.insert(salaryDTO.getSalary());

//        插入绩效信息
        Performance performance = new Performance();
        performance.setStaffId(salaryDTO.getSalary().getStaffId());
        performance.setAllPerformance(salaryDTO.getAllPerformance());
        performance.setGmtCreate(now);
        performance.setGmtModified(now);
        performance.setTimePerformance(now);
        performanceMapper.insert(performance);
//        插入考勤信息
        Attendance attendance = new Attendance();
        attendance.setStaffId(salaryDTO.getSalary().getStaffId());
        attendance.setAbsenceCount(salaryDTO.getAbsenceCount());
        attendance.setGmtCreate(salaryDTO.getSalary().getGmtCreate());
        attendance.setGmtModified(salaryDTO.getSalary().getGmtModified());
        attendance.setHoliday(salaryDTO.getHoliday());
        attendanceMapper.insert(attendance);

        
    }

    @Override
    public void deleteSalaryById(int id) {
        Salary salary = salaryMapper.selectById(id);
        if(salary == null){
            throw new NotFoundException("该用户的工资信息已经被删除");
        }
        salaryMapper.deleteById(id);

    }

    @Override
    public void updateSalary(SalaryVO salaryVO) {

        Salary salary = salaryMapper.selectById(salaryVO.getId());
        if(salary == null){
            throw new MessageExist("该员工不存在");
        }
        if(salaryVO.getSalary() == null){
            salaryVO.setSalary(salary.getSalary());
        }
        if(salaryVO.getCardNo() == null){
            salaryVO.setCardNo(salary.getCardNo());
        }
        if(salaryVO.getLevel() == null){
            salaryVO.setLevel(salary.getLevel());
        }
        if(salaryVO.getState() == null){
            salaryVO.setState(salary.getState());
        }
        System.out.println("111111111");
        salaryMapper.updateSalary(salaryVO);

    }

    @Override
    public SalaryVO findSalary(Integer id) {

        SalaryVO salaryVO = salaryMapper.selectBySalaryId(id);
        if(salaryVO == null){
            throw new NotFoundException("该用户工资信息不存在");
        }
        return salaryVO;

    }

}
